# - Find boost libraries
#

# This module defines
#  BOOST_INCLUDE_DIR, location of boost headers
#  BOOST_LIBRARY_DIR, location of boost libraries
#  BOOST_XXX_LIBRARY, the library to link against BOOST_XXX lib

# also defined, but not for general use are
#  BOOST_LIBRARY_SUFFIX, suffix to try for finding the boost libraries (unix only)

find_package(SearchUtils)
find_package(ExtraLibDir)

FIND_LIB_IN_EXTRALIBS(BOOST *boost* boost lib)

SET(BOOST_SEARCH_PATHS
	${BOOST_EXTRALIB_BASE_DIRS}
	/usr/include
	/usr/local/include
	"C:/Program Files/boost"
	"C:/Program Files"
	"D:/Program Files/boost"
	"D:/Program Files"
	"C:/Boost"
	"D:/Prog/Boost"
)
GLOB_PATHS(BOOST_SEARCH_PATHS
	GLOBS "boost_[0-9_]*"
	BASES ${BOOST_SEARCH_PATHS}
)
GLOB_PATHS(BOOST_SEARCH_PATHS
	GLOBS "include"
	BASES ${BOOST_SEARCH_PATHS}
)		
FIND_PATH(BOOST_INCLUDE_DIR
	NAMES boost/config.hpp
	PATHS ${BOOST_SEARCH_PATHS}
)

IF(NOT EXISTS "${BOOST_LIBRARY_DIR}" OR NOT IS_DIRECTORY "${BOOST_LIBRARY_DIR}")
	FIND_PATH(BOOST_LIBRARY_DIR_PARENT
		NAMES lib
		PATHS "${BOOST_INCLUDE_DIR}" "${BOOST_INCLUDE_DIR}/.." "${BOOST_INCLUDE_DIR}/../.."
	)
	IF(EXISTS "${BOOST_LIBRARY_DIR_PARENT}/lib/")
		SET(BOOST_LIBRARY_DIR
			"${BOOST_LIBRARY_DIR_PARENT}/lib"
			CACHE PATH
			"path to boost library files"
		)
	ENDIF()
	MARK_AS_ADVANCED(BOOST_LIBRARY_DIR_PARENT)
ENDIF()

IF(WIN32 AND NOT MINGW)
	# depend on autolinking on windows
	SET(BOOST_THREAD_LIBRARY "")
	SET(BOOST_PROGRAM_OPTIONS_LIBRARY "")
	SET(BOOST_FILESYSTEM_LIBRARY "")
	SET(BOOST_SIGNALS_LIBRARY "")
	SET(BOOST_SERIALIZATION_LIBRARY "")
	SET(BOOST_SYSTEM_LIBRARY "")
	SET(BOOST_DATETIME_LIBRARY "")

ELSE()
	SET(BOOST_LIBRARY_SEARCH_PATHS
		"${BOOST_LIBRARY_DIR}"
		"${BOOST_EXTRALIB_LIBRARY_PATHS}"
		"${BOOST_INCLUDE_DIR}/lib"
		/usr/lib /usr/local/lib
	)

	IF(NOT BOOST_LIBRARY_SUFFIX)
		MESSAGE(STATUS "No BOOST_LIBRARY_SUFFIX")
		# TODO: autodetect BOOST_LIBRARY_SUFFIX somehow
	ENDIF()

	SET(BOOST_LIBRARY_SUFFIX
		-gcc41-mt
		CACHE STRING
		"suffix to use for boost libraries"
	)

	SET(BOOST_LINK_STATIC OFF CACHE BOOL "Link to boost libraries statically if possible (only works first time cmake is run).")
	MARK_AS_ADVANCED(BOOST_LINK_STATIC)

	set(CMAKE_FIND_LIBRARY_SUFFIXES_ORIG ${CMAKE_FIND_LIBRARY_SUFFIXES})
	if(BOOST_LINK_STATIC)
		set(CMAKE_FIND_LIBRARY_SUFFIXES ".a;.so")
	endif()

	FIND_LIBRARY(BOOST_THREAD_LIBRARY
		NAMES boost_thread-mt boost_thread${BOOST_LIBRARY_SUFFIX}
		PATHS ${BOOST_LIBRARY_SEARCH_PATHS}
	)

	FIND_LIBRARY(BOOST_PROGRAM_OPTIONS_LIBRARY
		NAMES boost_program_options-mt boost_program_options${BOOST_LIBRARY_SUFFIX}
		PATHS ${BOOST_LIBRARY_SEARCH_PATHS}
	)

	FIND_LIBRARY(BOOST_FILESYSTEM_LIBRARY
		NAMES boost_filesystem-mt boost_filesystem${BOOST_LIBRARY_SUFFIX}
		PATHS ${BOOST_LIBRARY_SEARCH_PATHS}
	)

	FIND_LIBRARY(BOOST_SIGNALS_LIBRARY
		NAMES boost_signals-mt boost_signals${BOOST_LIBRARY_SUFFIX}
		PATHS ${BOOST_LIBRARY_SEARCH_PATHS}
	)

	FIND_LIBRARY(BOOST_SERIALIZATION_LIBRARY
		NAMES boost_serialization-mt boost_serialization${BOOST_LIBRARY_SUFFIX}
		PATHS ${BOOST_LIBRARY_SEARCH_PATHS}
	)

	FIND_LIBRARY(BOOST_SYSTEM_LIBRARY
		NAMES boost_system-mt boost_system${BOOST_LIBRARY_SUFFIX}
		PATHS ${BOOST_LIBRARY_SEARCH_PATHS}
	)

	FIND_LIBRARY(BOOST_DATETIME_LIBRARY
		NAMES boost_date_time-mt boost_date_time${BOOST_LIBRARY_SUFFIX}
		PATHS ${BOOST_LIBRARY_SEARCH_PATHS}
	)

	set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_ORIG})
ENDIF()

IF(WIN32 AND NOT MINGW)
	#MARK_AS_ADVANCED(BOOST_LIBRARY_DIR)
ELSE()
	IF(BOOST_INCLUDE_DIR)
		MESSAGE(STATUS "Found Boost include dir")
	ENDIF()

	IF(BOOST_LIBRARY_DIR)
		MESSAGE(STATUS "Found Boost library dir")
	ENDIF()

	IF(BOOST_THREAD_LIBRARY)
		MESSAGE(STATUS "Found Boost Thread library")
		IF(NOT MINGW AND NOT ANDROID)
			SET(BOOST_THREAD_LIBRARY "${BOOST_THREAD_LIBRARY}" pthread)
		ENDIF()
	ENDIF()

	IF(BOOST_SYSTEM_LIBRARY)
		MESSAGE(STATUS "Found Boost System library")
		IF(MINGW)
			SET(BOOST_SYSTEM_LIBRARY "${BOOST_SYSTEM_LIBRARY}" mswsock)
		ENDIF()
	ENDIF()

	IF(BOOST_PROGRAM_OPTIONS_LIBRARY)
		MESSAGE(STATUS "Found Boost Program Options")
	ENDIF()

	IF(BOOST_FILESYSTEM_LIBRARY)
		MESSAGE(STATUS "Found Boost File System")
	ENDIF()

	IF(BOOST_SIGNALS_LIBRARY)
		MESSAGE(STATUS "Found Boost Signals")
	ENDIF()

	IF(BOOST_SERIALIZATION_LIBRARY)
		MESSAGE(STATUS "Found Boost Serialization")
	ENDIF()

	IF(BOOST_DATETIME_LIBRARY)
		MESSAGE(STATUS "Found Boost DateTime")
	ENDIF()
ENDIF()
